--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

Use("common/Const");
Use("common/Log");
Use("common/Task");
Use("common/Pet");
Use("common/Skill");
Use("tasks/Charge");

-- I am a Tier 3 Swordsman.
local name = "F03";
local Initialize, status = loadfile(PERSONALITY_SWORDMAN);
Info(name .. ": trying fencer personality");
if (Initialize == nil) then
  Info(name .. ": trying default personality (" .. status .. ")");
  Initialize, status = loadfile(PERSONALITY_DEFAULT);
  if (Initialize == nil) then
    Alert(name .. ": " .. status);
    return ME;
  end
end

-- Initialize base personality.
--local instance = Initialize(...);
--local instance = Initialize(arg[1]);
local instance = Initialize();



--[[ Select an attack for this target ]]--
function instance:Attack(_target)
  -- Calculate overall danger level.
  local danger = self:Assess(ACTORS) + self.owner:Assess(ACTORS);
  local sp = GetV(V_SP, self.id);

  -- Attack normally unless it's crowded.
  local skill = Skill.None(self.id);

  -- Adjust danger by tranquility.
  danger = danger / self:GetTranquility();

  if (self.timer:MeasureRemaining() < 0) then
    -- Start another combo once the quicken skill finishes.
    self.combo = 0;
  end

  if (self.combo == 0) then
    -- MINIMUM SP: 14 for Weapon Quicken
    if (sp >= 14) then
      if (sp > (self.maxsp * (1.0 - danger))) then
        -- Force combo to position 1.
        self.combo = 1;

        -- Level 1 Weapon Quicken lasts 30 seconds.
        -- Note that our target is still the original foe.
        self.target = _target;
        self.task = Do.Create(self, Skill.Create(self.id, SKILL_WEAPONQUICKEN, 1), self.id);
        Info(name .. ": Buff " .. self.task.skill.code .. " level " .. self.task.skill.level .. " (danger=" .. danger .. ", combo=" .. self.combo .. ")");

        -- Update the checkpoint timestamp and delay.
        self.timer = Countdown.Create(30000);
        return;
      end
    end
  end

  -- Execute generic atatck.
  self.target = _target;
  self.task = Charge.Create(self, skill, self.target);
end

--[[ The pet should help the given target ]]--
-- We cast Benediction on the target. A minumum 6 second delay
-- applies between casts.
function instance:Help(_target)
  if (self.timer:MeasureElapsed() > 6000) then
    -- Start another combo once the quicken skill finishes.
    self.combo = 2;
  end

  if (self.combo == 2) then
    -- Force combo to position 3.
    self.combo = 3;

    -- Cast Benediction on the target.
    self.task = Do.Create(self, Skill.Create(self.id, SKILL_BENEDICTION, 1), _target);
    Info(name .. ": assisting " .. _target .. " with " .. self.task.skill.code .. " level " .. self.task.skill.level .. " (combo=" .. self.combo .. ")");

    -- Update the checkpoint timestamp and delay.
    self.timer = self.timer:Mark();
  else
    Persona.Help(self, _target);
  end
end


Info(name .. " Loaded");
return instance;
